package com.xz.xzutils.module.xzmybatislog;

import com.intellij.execution.ExecutionListener;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.ui.RunContentDescriptor;
import com.intellij.execution.ui.RunnerLayoutUi;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.content.Content;
import com.xz.xzutils.icons.MyIcons;
import com.xz.xzutils.utils.MyBatisDetector;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

/**
 * <p>
 * RUN/DEBUG 模式启动监听器
 * 可以在程序启动用时做一些操作
 * </p>
 *
 * @author DONG.WANG17@HAND-CHINA.COM
 * @since 2025-01-14 14:34
 */
public class RunExecutionListener implements ExecutionListener {
    private static final Logger LOGGER = Logger.getInstance(RunExecutionListener.class);

    @Override
    public void processStarted(@NotNull String executorId, @NotNull ExecutionEnvironment env, @NotNull ProcessHandler handler) {
        // 判断项目是否使用 mybatis
        if (!MyBatisDetector.isMyBatisUsed(env.getProject())) {
            LOGGER.info("processStarted 项目未使用 mybatis");
            return;
        }
        LOGGER.info("当前项目使用 mybatis，开启 mybatis 日志查看");
        LOGGER.info("processStarted 项目启动成功\t{}" + executorId);
        // 监听 RUN/DEBUG 模式启动
        if ("Run".equals(executorId) || "Debug".equals(executorId)) {
            RunContentDescriptor descriptor = env.getContentToReuse();
            if (null == descriptor) {
                return;
            }
            addCustomTab(env.getProject(), descriptor);
        }
    }
    // 模块

    private void addCustomTab(@NotNull Project project, @NotNull RunContentDescriptor descriptor) {
        if (null != descriptor.getRunnerLayoutUi()) {
            XzMybatisLogManager mybatisLogManager = XzMybatisLogManager.getInstance(project);
            JComponent consolePanel = mybatisLogManager.getConsolePanel();

            // 获取运行窗口 ID，并添加自定义面板
            final String toolWindowId = descriptor.getContentToolWindowId();
            final ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(toolWindowId);
            if (toolWindow != null) {
                final Content content = toolWindow.getContentManager().getFactory().createContent(consolePanel, "Sql From Mybatis Log", false);
                content.setCloseable(false);
                toolWindow.getContentManager().addContent(content);
            }

            // 在运行窗口中添加自定义的 sql 查看面板
            //RunnerLayoutUi runnerLayoutUi = descriptor.getRunnerLayoutUi();
            //Content content = runnerLayoutUi.createContent("Sql From Mybatis log", consolePanel, "Sql From Mybatis Log", MyIcons.PLUGIN_ICONS, consolePanel);
            //content.setCloseable(false);
            //runnerLayoutUi.addContent(content);

            // 缓存 XzMybatisLogManager 对象
            project.putUserData(XzMybatisLogManager.KEY, mybatisLogManager);
        }
    }
}
